/**
 * @author yxw
 * date : 2013年9月28日 下午11:37:48 
 */
package com.tzinfo.tzt.activity;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.tzinfo.data.TztData;
import com.tzinfo.tzb.TzbMainActivity;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.util.GPSThread;
import com.tzinfo.tzt.activity.util.MyApplication;
import com.tzinfo.tztbase.TztBaseMainActivity;
import com.tzinfo.tztbase.index.TztBaseIndex;
import com.yxw.util.OutLog;

public class WelcomeActivity extends BaseActivity {
    private boolean isFirstLoad = true;
    private String version;
    private Handler mHandler;
    private boolean isConn = true;
    private DisplayMetrics dm;
    private Intent intentFromServerPush = null;
    public final static String Subject_PUSH = "subject";
    public final static String House_PUSH = "house";
    private String baiduUserId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 已在AndroidManifest.xml中配置全屏显示
        // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        // WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉状态栏
        // requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        setContentView(R.layout.activity_welcome);

        // 获取手机的分辨率
        dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        MyApplication.widthPixels = dm.widthPixels;
        MyApplication.heightPixels = dm.heightPixels;
        intentFromServerPush = getIntent();
//		getLocation();
//        Intent intent = new Intent();
//        intent.putExtra("open_flag","new_message");
//        intent.putExtra("fid","2");
//        intent.putExtra("tid","786450");
//        intent.setClass(this, WelcomeActivity.class);
//        OutLog.LogE("urintentl", intent.toURI());
        RegisterReceiver();
        initView();
        GPSThread.getInstance(this).requestLocation();
    }

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // internet lost alert dialog method call from here...
            baiduUserId = intent.getStringExtra("data");
        }
    };

    public void RegisterReceiver() {
        registerReceiver(broadcastReceiver, new IntentFilter("Intent_baiduUserId_bind"));
        PushManager.startWork(getApplicationContext(),
                PushConstants.LOGIN_TYPE_API_KEY, TztData.baiduPushApiKey);
    }

    private void unRegisterReceiver() {
        try {
            unregisterReceiver(broadcastReceiver);
        } catch (Exception e) {

        }
    }

    @Override
    public void finish() {
        unRegisterReceiver();
        super.finish();
    }

    protected void getError(String ERRORMESSAGE) {
        new AlertDialog.Builder(this)
                .setCancelable(false)
                .setMessage(ERRORMESSAGE)
                .setPositiveButton("退出", new DialogInterface.OnClickListener()

                {
                    public void onClick

                            (DialogInterface dialog, int which) {
                        finish();
                    }
                })
                .setNegativeButton("直接进入系统",
                        new DialogInterface.OnClickListener()

                        {
                            public void onClick(

                                    DialogInterface dialogInterface, int

                                    whichButton) {
                                MyApplication.locationCode = 0;
                                checkIfFirstLogin();
                                if (isFirstLoad) {// 如果是首次运行
                                    sharedPreferences.saveMessage("isFirstLoad"
                                            + version, false);
//									goGuideSwitchViewActivity();
                                    Toast.makeText(WelcomeActivity.this, "您第一次登录系统", Toast.LENGTH_SHORT).show();
                                    goIndexActivity();
                                } else {// 如果不是首次运行
                                    goIndexActivity();
                                }
                            }
                        }).show();
    }

    /**
     * 进入主界面
     */
    public void goIndexActivity() {
        unRegisterReceiver();
        Intent intent = new Intent();
        TztBaseIndex.activtyHouse = MainActivity.class;
        TztBaseIndex.activtyBang = TzbMainActivity.class;
        TztBaseIndex.activtyCar = CarPlateActivity.class;
        intent.setClass(this, TztBaseMainActivity.class);
        if (Subject_PUSH.equals(intentFromServerPush.getStringExtra("open_flag"))) {
            intent.putExtra("open_flag", intentFromServerPush.getStringExtra("open_flag"));
            intent.putExtra("fid", intentFromServerPush.getStringExtra("fid"));
            intent.putExtra("tid", intentFromServerPush.getStringExtra("tid"));
            intent.putExtra("Subject", intentFromServerPush.getStringExtra("Subject"));
            startActivity(intent);
        }
        else if (House_PUSH.equals(intentFromServerPush.getStringExtra("open_flag"))) {
            intent.putExtra("open_flag", intentFromServerPush.getStringExtra("open_flag"));
            intent.putExtra("houseNewsId", intentFromServerPush.getStringExtra("houseNewsId"));
            startActivity(intent);
        } else {
            startActivity(intent);
        }
        finish();
    }

    /**
     * 进滑动介绍页面
     */
    public void goGuideSwitchViewActivity() {
        Intent intent = new Intent();
        intent.setClass(this, GuideSwitchViewActivity.class);
        startActivity(intent);
        finish();
    }

    public void initView() {

        mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
            public void run() {
                if (isConn) {
                    checkIfFirstLogin();
                    if (isFirstLoad) {// 如果是首次运行
                        sharedPreferences.saveMessage("isFirstLoad" + version,
                                false);
//						goGuideSwitchViewActivity();
                        Toast.makeText(WelcomeActivity.this, "您第一次登录系统", Toast.LENGTH_SHORT).show();
                        //测试阶段不进入向导页面
                        goIndexActivity();
                    } else {// 如果不是首次运行
                        goIndexActivity();
                    }
                }
            }
        }, 1000);
    }

    protected void onDestroy() {
        super.onDestroy();
        unRegisterReceiver();
    }

    private void checkIfFirstLogin() {
        try {
            PackageManager packageManager = getPackageManager();
            PackageInfo packInfo = packageManager.getPackageInfo(
                    getPackageName(), 0);
            version = packInfo.versionName;
        } catch (NameNotFoundException e) {
            Log.e("NameNotFoundException", e.getMessage());
        }
        isFirstLoad = sharedPreferences.readBooleanMessage("isFirstLoad"
                + version);
    }

}


